package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class InsertSort {

	public static void main(String[] args) {
		int[] arr = {101, 34, 119, 1, -1, 89}; 
		
		
		System.out.println(Arrays.toString(arr));
	
		Date data1 = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1Str = simpleDateFormat.format(data1);
		System.out.println("开始时间=" + date1Str);
		
		insertSort(arr); 
		
		Date data2 = new Date();
		String date2Str = simpleDateFormat.format(data2);
		System.out.println("结束时间=" + date2Str);
		
	}
	
	
	public static void insertSort(int[] arr) {
		int insertVal = 0;
		int insertIndex = 0;
		
		for(int i = 1; i < arr.length; i++) {
			insertVal = arr[i];
			insertIndex = i - 1; 	
			while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
				arr[insertIndex + 1] = arr[insertIndex];// arr[insertIndex]
				insertIndex--;
			}
			
			if(insertIndex + 1 != i) {
				arr[insertIndex + 1] = insertVal;
			}
	
			System.out.println(i);
			System.out.println(Arrays.toString(arr));
		}
		
		
		
	}

}
